package com.lkd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.SkuDTO;
import com.lkd.dto.SkuQueryDTO;
import com.lkd.entity.Sku;
import com.lkd.mapper.SkuClassMapper;
import com.lkd.mapper.SkuMapper;
import com.lkd.result.TableInfo;
import com.lkd.service.SkuService;
import com.lkd.vo.SkuVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Z.lx
 * @Date: 2025/8/17 08:54
 * @Description:
 **/
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    private final SkuMapper skuMapper;
    private final SkuClassMapper skuClassMapper;

    @Autowired
    public SkuServiceImpl(SkuMapper skuMapper, SkuClassMapper skuClassMapper) {
        this.skuMapper = skuMapper;
        this.skuClassMapper = skuClassMapper;
    }

    /**
    * @Description 新增
    * @Param [skuDto]
    * @Date 2025/8/17 9:56
    **/
    public void save(SkuDTO skuDto) {
        Sku sku = new Sku();
        BeanUtils.copyProperties(skuDto,sku);
        sku.setCreateTime(LocalDateTime.now());
        sku.setUpdateTime(LocalDateTime.now());
        skuMapper.insert(sku);
    }

    /**
     * @Description 列表查询
     * @Param [skuDto]
     * @Date 2025/8/17 9:56
     **/
    public TableInfo<Sku> list(SkuQueryDTO skuDto) {
        PageHelper.startPage(skuDto.getPageIndex(), skuDto.getPageSize());
        List<Sku> skuList =skuMapper.selectSkuList(skuDto.getSkuName());
        Page<Sku> page = (Page<Sku>) skuList;
        return  new TableInfo<>(page.getPageNum(),page.getPageSize(),page.getPages(),page.getTotal(),page.getResult());
    }

    @Override
    public List<SkuVO> listTop10(String businessId) {
        return skuMapper.listTop10(businessId);
    }

    /**
     * @Description 更新商品
     * @Param [skuId, skuDto]
     * @Date 2025/8/17 10:59
     **/
    public void updateSku(SkuDTO skuDto) {
        Sku sku = new Sku();
        BeanUtils.copyProperties(skuDto,sku);
        skuMapper.updateSku(sku);
    }
}
